CHAPTER 7

Interfaces

An interface defines a contract between components. A contract, when applied to a type, imposes a set of requirements on that type. Typically, this means a set of methods and proper-ties that any type implementing the interface is guaranteed to provide. But, contracts aren’t the only thing interfaces provide. Since Visual Basic (VB) (wisely) doesn’t support inheritance from multiple types but does allow types to implement multiple interfaces, interfaces are the major foundation for polymorphic programming.

Interfaces Are Reference Types

An interface defines a reference type, but, unlike classes, interfaces cannot be instantiated. Classes and structures implement interfaces—that is, they define the methods and other members that form the contract defined by the interface. Variables of an interface type can hold a reference to any object that implements the interface.

Take a look at the following artificial but nonetheless typical pattern:

Public Interface IUIControl
    Sub Paint()
End Interface

Public Class Button
    Implements IUIControl
    Public Sub Paint() Implements IUIControl.Paint

        'Paint the Button
    End Sub
End Class

Public Class ListBox
    Implements IUIControl
    Public Sub Paint() Implements IUIControl.Paint
        'Paint the Listbox
    End Sub
End Class

This example declares an interface named IUIControl that simply exposes one method, Paint. This interface defines a contract, which states that any type that implements this inter-face must implement the Paint method.

Since the classes ListBox and Button implement the interface, you can treat them both as of type IUIControl. You can store any instance of either Button or ListBox in a variable declared as IUIControl. The references to objects of these class types are implicitly convertible to the IUIControl type. However, to convert an IUIControl reference back into a ListBox or Button reference requires an explicit conversion, and that coercion will fail if the object pointed to by the IUIControl reference is not of the type specified by the conversion.

Note: It’s useful to name methods according to both the action they perform and where the action is directed. For example, suppose the IUIControl.Paint method takes a Graphics object as a parameter telling it where to paint itself. The code is more readable if the method is named IUIControl.PaintSelfTo(). This way, the method call sort of reads like a spoken language in the sense that a method call that looks like control.PaintSelfTo( myGraphicsObject ) is saying, “control, please paint yourself to myGraphicsObject.”

Defining Interfaces

Interface declarations are similar to class declarations, but interfaces cannot declare fields, and they can only declare, but cannot implement, other members. For example, in the following code

Interface IUIControl
    Sub Paint()
End Interface

IUIControl has only one member, the method Paint, and it uses only a Sub statement to declare it, without providing a method body or an End Substatement.

Interfaces, like classes, default to Friend accessibility in a namespace, but you can also declare them Public. Within classes, modules, interfaces, and structures, they default to Public, but they can also be Friend, Protected, or Private. Interface members are implicitly Public and may not have access modifiers.

Note: By convention, interface names start with I.